package com.leff.cards.ui.util;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.leff.cards.R;
import com.leff.cards.data.AppState;
import com.leff.cards.data.HistoryItem;

public class HistoryListAdapter extends BaseExpandableListAdapter {

	private Context mContext;
	private ArrayList<HistoryItem> mData;
	
	public HistoryListAdapter(Context C) {
		mContext = C;
		mData = AppState.get().getActiveGroup().history;
	}
	@Override
	public Object getChild(int group, int position) {
		return mData.get(group).responses.get(position);
	}

	@Override
	public long getChildId(int group, int position) {
		return position;
	}

	@Override
	public View getChildView(int group, int position, boolean isLast, View convertView, ViewGroup parent) {
		View V = convertView;
		
		HistoryItem HI = mData.get(group);
		String card = HI.responses.get(position);
		
		if(V == null) {
			V = LayoutInflater.from(mContext).inflate(R.layout.response_card_view, null);
		}
		
		TextView text = (TextView)V.findViewById(R.id.card_text);
		text.setTextColor(Color.BLACK);
		if(card.equals(HI.chosenResponse)) {
			text.setTextColor(Color.GREEN);
		}
		
		text.setText(card);
		
		return V;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return mData.get(groupPosition).responses.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return mData.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return mData.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,	View convertView, ViewGroup parent) {
		View V = convertView;
		HistoryItem HI = mData.get(groupPosition);
		
		if(V == null) {
			V = LayoutInflater.from(mContext).inflate(R.layout.response_card_view, null);
		}
		
		TextView text = (TextView)V.findViewById(R.id.card_text);
		
		V.setBackgroundColor(Color.BLACK);
		text.setTextColor(Color.WHITE);
		text.setText(HI.query);
		
		return V;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

}
